package com.nl.controller.sms5g.controller;

import com.alibaba.fastjson.JSON;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.util.BaseResult;
import com.nl.controller.sms5g.bean.bo.*;
import com.nl.controller.sms5g.bean.po.QueryRangeResp;
import com.nl.controller.sms5g.bean.po.Sms5gGetStationsResp;
import com.nl.controller.sms5g.service.Sms5gRangeStationsService;
import com.nl.controller.sms5g.service.Sms5gSendRangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @desc
 * @return null
 * @author l
 * @date 2021/6/8 19:35
 */
@RestController
@Slf4j
@Validated
@RequestMapping("/sendRange")
public class Sms5gSendRangeController {
    
    @Autowired
    private Sms5gSendRangeService sms5gSendRangeService;

    @Autowired
    private Sms5gRangeStationsService sms5gRangeStationsService;
    
    /**
     * @desc 新增5G项目发送范围基站信息
     * @param insertBaseStationBo 
     * @return com.nl.controller.common.util.BaseResult
     * @author l
     * @date 2021/6/17 17:47
     */
    @RequestMapping("/createRangeStation")
    public BaseResult createRangeStation(@RequestBody @Validated InsertBaseStationBo insertBaseStationBo){
        BaseResult baseResult = sms5gSendRangeService.createRangeStation(insertBaseStationBo);
        return  baseResult;
    }

    @RequestMapping("/updateRange")
    public BaseResult<Object> updateRange(@Validated @RequestBody UpdateRangeReq req, BindingResult bindingResult){
        log.info("Sms5gSendRangeController updateRange 更新发送范围 start request: "+ JSON.toJSONString(req));
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        BaseResult<Object> result = sms5gSendRangeService.updateRange(req);
        log.info("Sms5gSendRangeController updateRange 更新发送范围 end response: "+ JSON.toJSONString(result));
        return result;
    }

    @RequestMapping("/createRange")
    public BaseResult<Object> createRange(@Validated @RequestBody CreateRangeReq req, BindingResult bindingResult){
        log.info("Sms5gSendRangeController createRange 新增发送范围 start request: "+ JSON.toJSONString(req));
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        BaseResult<Object> result = sms5gSendRangeService.createRange(req);
        log.info("Sms5gSendRangeController createRange 新增发送范围 end response: "+ JSON.toJSONString(result));
        return result;
    }


    @RequestMapping("/queryRange")
    public BaseResult<QueryRangeResp> queryRange(@Validated @RequestBody QuerySendRangeBo req, BindingResult bindingResult){
        log.info("queryRange 点位信息查询 start request: "+ JSON.toJSONString(req));
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        BaseResult<QueryRangeResp> result = sms5gSendRangeService.queryRange(req);
        log.info("queryRange 点位信息查询 end response: "+ JSON.toJSONString(result));
        return result;
    }


    @RequestMapping("/getStations")
    public BaseResult<Sms5gGetStationsResp> getStations(@RequestBody Sms5gGetStationsReq req){
        log.info("Sms5gSendRangeController getStations  start request: "+ JSON.toJSONString(req));
        try {
            //圈画范围经纬度信息
            if(!StringUtils.isEmpty(req.getLongAndLatListInfo())) {
                List<String> longAndLatList = JSON.parseArray(req.getLongAndLatListInfo(), String.class);
                req.setLongAndLatList(longAndLatList);
            }
        } catch(Exception e) {
            throw new SmsException(e.getMessage());
        }
        try {
            //选中点位名称
            if(!StringUtils.isEmpty(req.getPointNames())) {
                List<String> pointNameList = JSON.parseArray(req.getPointNames(), String.class);
                req.setPointNameList(pointNameList);
            }
        } catch(Exception e) {
            throw new SmsException(e.getMessage());
        }
        BaseResult<Sms5gGetStationsResp> result = sms5gRangeStationsService.xuYiGetStations(req);
        log.info("Sms5gSendRangeController getStations end response: "+ JSON.toJSONString(result));
        return result;
    }


}
